import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import store from '../store'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      alias: '/home',
      component: () => import(/* webpackChunkName: "Home" */ '../views/Home.vue')
    },
    {
      path: '/home/:id?',
      name: 'Home',
      component: () => import(/* webpackChunkName: "Home" */ '../views/Home.vue')
    },
    {
      path: '/vote-column',
      name: 'VoteColumn',
      component: () => import(/* webpackChunkName: "VoteColumn" */ '../views/VoteColumn.vue')
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue')
    },
    {
      path: '/thank',
      name: 'Thank',
      component: () => import(/* webpackChunkName: "Login" */ '../views/Thank.vue')
    },
    {
      path: '/result/:id?',
      name: 'Statistic',
      component: () => import(/* webpackChunkName: "Statistic" */ '../views/Statistic.vue')
    },
    {
      path: '/HelloWorld',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (store.getters.userId === null && to.name !== 'Login') {
    next({path: '/login'})
  }
  next()
})

export default router
